﻿/**
 * Created by chesw on 2017/5/3.
 */
$.ajaxSetup({
    cache: false //关闭AJAX缓存
});
var userName;
(function () {
    var vm = new Vue({
        el: '#homepage',
        data: {
            coorperateType: 0, //0:医疗行业，1：金融行业，2：国家机关，3：教育机构，4：国有企业，5：外资企业，6：民营企业
            showMid: true,
            emptyResult: false,
            showLoad:false,
            userName: '',
            searchKey: '',
            isFirstLogoImage: '',
            isMondayInvalid: "date-invalid",
            isMondayDisable: "",
            isTuesdayInvalid: "date-invalid",
            isTuesdayDisable: "",
            isWednesdayInvalid: "date-invalid",
            isWednesdayDisable: "",
            isThursdayInvalid: "date-invalid",
            isThursdayDisable: "",
            isFridayInvalid: "date-invalid",
            isFridayDisable: "",
            isSaturdayInvalid: "date-invalid",
            isSaturdayDisable: "",
            isLastWeekDisable:"",
            homeLogoImageList:[],
            articList: [],
            newsList: [],
            clinicsNewsList:[],
            videoList: [],
            guideList:[],
            doctorList: [],
            doctorInfo: { Id: "", Name: '', Image: '', DeptName: '', DepName2: '', DeptID: '', TitleName: '', OrgName: '', AffiliatedHospital: '', SkillIn: '',keyWord:'' },
            loginInfo: { isLogin: '', name: '', mobile: '' },
            dateWeekModel: [{ Monday: '' }, { Tuesday: '' }, { Wednesday: '' }, { Thursday: '' }, { Friday: '' }, { Saturday: '' }],
            noticeLeft: 0,
            allCooperate: {
                0: [
                    { url: '../../Styles/image/mb-1.png' }, { url: '../../Styles/image/mb-2.png' },
                    { url: '../../Styles/image/mb-3.png' }, { url: '../../Styles/image/mb-4.png' },
                    { url: '../../Styles/image/mb-5.png' }, { url: '../../Styles/image/mb-6.png' },
                    { url: '../../Styles/image/mb-7.png' }, { url: '../../Styles/image/mb-8.png' },
                    { url: '../../Styles/image/mb-9.png' }, { url: '../../Styles/image/mb-10.png' }
                ],
                1: [
                    { url: '../../Styles/image/fi-1.png' }, { url: '../../Styles/image/fi-2.png' },
                    { url: '../../Styles/image/fi-3.png' }, { url: '../../Styles/image/fi-4.png' },
                    { url: '../../Styles/image/fi-5.png' }, { url: '../../Styles/image/fi-6.png' },
                    { url: '../../Styles/image/fi-7.png' }, { url: '../../Styles/image/fi-8.png' },
                    { url: '../../Styles/image/fi-9.png' }, { url: '../../Styles/image/fi-10.png' },
                    { url: '../../Styles/image/fi-11.png' }, { url: '../../Styles/image/fi-12.png' },
                    { url: '../../Styles/image/fi-13.png' }, { url: '../../Styles/image/fi-14.png' },
                    { url: '../../Styles/image/fi-15.png' }
                ],
                2: [
                    { url: '../../Styles/image/so-1.png' }, { url: '../../Styles/image/so-2.png' },
                    { url: '../../Styles/image/so-3.png' }, { url: '../../Styles/image/so-4.png' },
                    { url: '../../Styles/image/so-5.png' }
                ],
                3: [
                    { url: '../../Styles/image/es-1.png' }, { url: '../../Styles/image/es-2.png' },
                    { url: '../../Styles/image/es-3.png' }, { url: '../../Styles/image/es-4.png' },
                    { url: '../../Styles/image/es-5.png' }
                ],
                4: [
                    { url: '../../Styles/image/soe-1.png' }, { url: '../../Styles/image/soe-2.png' },
                    { url: '../../Styles/image/soe-3.png' }, { url: '../../Styles/image/soe-4.png' },
                    { url: '../../Styles/image/soe-5.png' }, { url: '../../Styles/image/soe-6.png' },
                    { url: '../../Styles/image/soe-7.png' }, { url: '../../Styles/image/soe-8.png' },
                    { url: '../../Styles/image/soe-9.png' }, { url: '../../Styles/image/soe-10.png' }
                ],
                5: [
                    { url: '../../Styles/image/fe-1.png' }, { url: '../../Styles/image/fe-2.png' },
                    { url: '../../Styles/image/fe-3.png' }, { url: '../../Styles/image/fe-4.png' },
                    { url: '../../Styles/image/fe-5.png' }, { url: '../../Styles/image/fe-6.png' },
                    { url: '../../Styles/image/fe-7.png' }, { url: '../../Styles/image/fe-8.png' },
                    { url: '../../Styles/image/fe-9.png' }, { url: '../../Styles/image/fe-10.png' }
                ],
                6: [
                    { url: '../../Styles/image/po-1.png' }, { url: '../../Styles/image/po-2.png' },
                    { url: '../../Styles/image/po-3.png' }, { url: '../../Styles/image/po-4.png' },
                    { url: '../../Styles/image/po-5.png' }
                ]
            }
        },
        computed: {
            cooperateList: function () {
                return this.allCooperate[this.coorperateType];
            }
        },
        created: function () {
            //检查登录状态
            CheckLoginStatus(this, this.loginInfo);
            this.getBanner();
        },
        mounted: function () {
            showHtml();
            var _self = this;
            //公告和头部导航对齐
            _self.$nextTick(function () {
                _self.noticeLeft = (document.getElementsByClassName('clinic-banner')[0].clientWidth - 1190) / 2;
            });

            //设置导航栏样式
            document.getElementById("Home").className = "valid-nav";
            document.getElementById("OrderExpert").className = "invalid-nav";
            document.getElementById("DistinctMedical").className = "invalid-nav";
            document.getElementById("MedicalWiki").className = "invalid-nav";
            document.getElementById("News").className = "invalid-nav";
            document.getElementById("AboutUs").className = "invalid-nav";           
            //处理医生列表
            _self.dateWeekModel = [];
            _self.isMondayInvalid = "date-invalid",
            _self.isTuesdayInvalid = "date-invalid",
            _self.isWednesdayInvalid = "date-invalid",
            _self.isThursdayInvalid = "date-invalid",
            _self.isFridayInvalid = "date-invalid",
            _self.isSaturdayInvalid = "date-invalid";

            var p_searchKeys = getQueryString("searchKeys");
            
            if (p_searchKeys != null) {
                _self.$refs.header.setSearchKey(p_searchKeys);
                _self.globalSearch(p_searchKeys);
            }

            if (_self.showMid) {
                _self.$http.get("/Home/GetCurrentWeekDate").then(function (res) {
                    if (res.body != null) {
                        _self.dateWeekModel.push(res.body.Monday);
                        _self.dateWeekModel.push(res.body.Tuesday);
                        _self.dateWeekModel.push(res.body.Wednesday);
                        _self.dateWeekModel.push(res.body.Thursday);
                        _self.dateWeekModel.push(res.body.Friday);
                        _self.dateWeekModel.push(res.body.Saturday);
                        _self.dateWeekModel.push(res.body.Year);

                        if (res.body.Monday == res.body.SelectedDate) {
                            _self.isMondayInvalid = "date-valid";
                        } else if (res.body.Tuesday == res.body.SelectedDate) {
                            _self.isTuesdayInvalid = "date-valid";
                        } else if (res.body.Wednesday == res.body.SelectedDate) {
                            _self.isWednesdayInvalid = "date-valid";
                        } else if (res.body.Thursday == res.body.SelectedDate) {
                            _self.isThursdayInvalid = "date-valid";
                        } else if (res.body.Friday == res.body.SelectedDate) {
                            _self.isFridayInvalid = "date-valid";
                        } else if (res.body.Saturday == res.body.SelectedDate) {
                            _self.isSaturdayInvalid = "date-valid";
                        }

                        if (res.body.IsMondayDisable) {
                            _self.isMondayDisable = "date-disable";
                            _self.isLastWeekDisable = "date-disable";
                        } else {
                            _self.isMondayDisable = "";
                            _self.isLastWeekDisable = "";
                        }
                        if (res.body.IsTuesdayDisable) {
                            _self.isTuesdayDisable = "date-disable";
                        } else {
                            _self.isTuesdayDisable = "";
                        }
                        if (res.body.IsWednesdayDisable) {
                            _self.isWednesdayDisable = "date-disable";
                        }
                        else {
                            _self.isWednesdayDisable = "";
                        }
                        if (res.body.IsThursdayDisable) {
                            _self.isThursdayDisable = "date-disable";
                        }
                        else {
                            _self.isThursdayDisable = "";
                        }
                        if (res.body.IsFridayDisable) {
                            _self.isFridayDisable = "date-disable";
                        }
                        else {
                            _self.isFridayDisable = "";
                        }
                        if (res.body.IsSaturdayDisable) {
                            _self.isSaturdayDisable = "date-disable";
                        }
                        else {
                            _self.isSaturdayDisable = "";
                        }
                    }
                });
                _self.doctorList = [];
                this.$http.get("/Home/GetDoctorList").then(function (res) {
                    for (var i = 0; i < res.body.length; i++) {
                        _self.doctorList.push(res.body[i]);
                    }

                    overflowEllipsis(_self);
                });
                _self.newsList = [];
                this.$http.get("/Home/GetNewsLists").then(function (res) {
                    for (var i = 0; i < res.body.length; i++) {
                        _self.newsList.push(res.body[i]);
                    }
                });
                _self.articList = [];
                this.$http.get("/Home/GetArticLists").then(function (res) {
                    for (var i = 0; i < res.body.length; i++) {
                        _self.articList.push(res.body[i]);
                    }
                });
            }                 
        },
        methods: {
            GetNextWeekDate: function (date, year) {
                var _self = this;
                _self.dateWeekModel = [];
                _self.doctorList = [];
                _self.isMondayInvalid = "date-invalid",
                _self.isTuesdayInvalid = "date-invalid",
                _self.isWednesdayInvalid = "date-invalid",
                _self.isThursdayInvalid = "date-invalid",
                _self.isFridayInvalid = "date-invalid",
                _self.isSaturdayInvalid = "date-invalid";
                _self.$http.post("/Home/GetNextWeekDate", { datepoint: date, year: year }).then(function (res) {
                    if (res.body != null) {
                        _self.dateWeekModel.push(res.body.Monday);
                        _self.dateWeekModel.push(res.body.Tuesday);
                        _self.dateWeekModel.push(res.body.Wednesday);
                        _self.dateWeekModel.push(res.body.Thursday);
                        _self.dateWeekModel.push(res.body.Friday);
                        _self.dateWeekModel.push(res.body.Saturday);
                        _self.dateWeekModel.push(res.body.Year);

                        if (res.body.Monday == res.body.SelectedDate) {
                            _self.isMondayInvalid = "date-valid";
                        } else if (res.body.Tuesday == res.body.SelectedDate) {
                            _self.isTuesdayInvalid = "date-valid";
                        } else if (res.body.Wednesday == res.body.SelectedDate) {
                            _self.isWednesdayInvalid = "date-valid";
                        } else if (res.body.Thursday == res.body.SelectedDate) {
                            _self.isThursdayInvalid = "date-valid";
                        } else if (res.body.Friday == res.body.SelectedDate) {
                            _self.isFridayInvalid = "date-valid";
                        } else if (res.body.Saturday == res.body.SelectedDate) {
                            _self.isSaturdayInvalid = "date-valid";
                        }

                        if (res.body.IsMondayDisable) {
                            _self.isMondayDisable = "date-disable";
                            _self.isLastWeekDisable = "date-disable";
                        } else {
                            _self.isMondayDisable = "";
                            _self.isLastWeekDisable = "";
                        }
                        if (res.body.IsTuesdayDisable) {
                            _self.isTuesdayDisable = "date-disable";
                        } else {
                            _self.isTuesdayDisable = "";
                        }
                        if (res.body.IsWednesdayDisable) {
                            _self.isWednesdayDisable = "date-disable";
                        }
                        else {
                            _self.isWednesdayDisable = "";
                        }
                        if (res.body.IsThursdayDisable) {
                            _self.isThursdayDisable = "date-disable";
                        }
                        else {
                            _self.isThursdayDisable = "";
                        }
                        if (res.body.IsFridayDisable) {
                            _self.isFridayDisable = "date-disable";
                        }
                        else {
                            _self.isFridayDisable = "";
                        }
                        if (res.body.IsSaturdayDisable) {
                            _self.isSaturdayDisable = "date-disable";
                        }
                        else {
                            _self.isSaturdayDisable = "";
                        }

                        _self.$http.post("/Home/GetDoctorLists", { datepoint: res.body.SelectedDate, year: year }).then(function (res) {
                            for (var i = 0; i < res.body.length; i++) {
                                _self.doctorList.push(res.body[i]);
                            }
                            overflowEllipsis(_self);
                        });
                    }
                });
                return false;
            },
            GetLastWeekDate: function (date, year) {
                var _self = this;
                _self.dateWeekModel = [];
                _self.doctorList = [];
                _self.isMondayInvalid = "date-invalid",
                _self.isTuesdayInvalid = "date-invalid",
                _self.isWednesdayInvalid = "date-invalid",
                _self.isThursdayInvalid = "date-invalid",
                _self.isFridayInvalid = "date-invalid",
                _self.isSaturdayInvalid = "date-invalid";
                _self.$http.post("/Home/GetLastWeekDate", { datepoint: date, year: year }).then(function (res) {
                    if (res.body != null) {
                        _self.dateWeekModel.push(res.body.Monday);
                        _self.dateWeekModel.push(res.body.Tuesday);
                        _self.dateWeekModel.push(res.body.Wednesday);
                        _self.dateWeekModel.push(res.body.Thursday);
                        _self.dateWeekModel.push(res.body.Friday);
                        _self.dateWeekModel.push(res.body.Saturday);
                        _self.dateWeekModel.push(res.body.Year);

                        if (res.body.Monday == res.body.SelectedDate) {
                            _self.isMondayInvalid = "date-valid";
                        } else if (res.body.Tuesday == res.body.SelectedDate) {
                            _self.isTuesdayInvalid = "date-valid";
                        } else if (res.body.Wednesday == res.body.SelectedDate) {
                            _self.isWednesdayInvalid = "date-valid";
                        } else if (res.body.Thursday == res.body.SelectedDate) {
                            _self.isThursdayInvalid = "date-valid";
                        } else if (res.body.Friday == res.body.SelectedDate) {
                            _self.isFridayInvalid = "date-valid";
                        } else if (res.body.Saturday == res.body.SelectedDate) {
                            _self.isSaturdayInvalid = "date-valid";
                        }

                        if (res.body.IsMondayDisable) {
                            _self.isMondayDisable = "date-disable";
                            _self.isLastWeekDisable = "date-disable";
                        } else {
                            _self.isMondayDisable = "";
                            _self.isLastWeekDisable = "";
                        }
                        if (res.body.IsTuesdayDisable) {
                            _self.isTuesdayDisable = "date-disable";
                        } else {
                            _self.isTuesdayDisable = "";
                        }
                        if (res.body.IsWednesdayDisable) {
                            _self.isWednesdayDisable = "date-disable";
                        }
                        else {
                            _self.isWednesdayDisable = "";
                        }
                        if (res.body.IsThursdayDisable) {
                            _self.isThursdayDisable = "date-disable";
                        }
                        else {
                            _self.isThursdayDisable = "";
                        }
                        if (res.body.IsFridayDisable) {
                            _self.isFridayDisable = "date-disable";
                        }
                        else {
                            _self.isFridayDisable = "";
                        }
                        if (res.body.IsSaturdayDisable) {
                            _self.isSaturdayDisable = "date-disable";
                        }
                        else {
                            _self.isSaturdayDisable = "";
                        }

                        _self.$http.post("/Home/GetDoctorLists", { datepoint: res.body.SelectedDate, year: year }).then(function (res) {
                            for (var i = 0; i < res.body.length; i++) {
                                _self.doctorList.push(res.body[i]);
                            }

                            overflowEllipsis(_self);
                        });
                    }
                });
                return false;
            },
            GetDoctorLists: function (date, year,selectedday) {
                var _self = this;
                _self.doctorList = [];                
                _self.$http.post("/Home/GetDoctorLists", { datepoint: date, year: year }).then(function (res) {
                    for (var i = 0; i < res.body.length; i++) {
                        _self.doctorList.push(res.body[i]);
                    }
                    overflowEllipsis(_self);

                    if (selectedday != "") {
                        _self.isMondayInvalid = "date-invalid";
                        _self.isTuesdayInvalid = "date-invalid";
                        _self.isWednesdayInvalid = "date-invalid";
                        _self.isThursdayInvalid = "date-invalid";
                        _self.isFridayInvalid = "date-invalid";
                        _self.isSaturdayInvalid = "date-invalid";

                        if (selectedday == "Monday") {
                            _self.isMondayInvalid = "date-valid";
                        } else if (selectedday == "Tuesday") {
                            _self.isTuesdayInvalid = "date-valid";
                        } else if (selectedday == "Wednesday") {
                            _self.isWednesdayInvalid = "date-valid";
                        } else if (selectedday == "Thursday") {
                            _self.isThursdayInvalid = "date-valid";
                        } else if (selectedday == "Friday") {
                            _self.isFridayInvalid = "date-valid";
                        } else if (selectedday == "Saturday") {
                            _self.isSaturdayInvalid = "date-valid";
                        }
                    }
                });
                return false;
            },
            doctorBooking: function ($event) {
                var opensrc = "/ExpertDetail/Index?doctorid=" + $event.currentTarget.id;
                window.open(opensrc);
            },
            homeLogoImageOpen: function ($event) {
                var opensrc = $event.currentTarget.id;
                window.open(opensrc);
            },
            globalSearch: function (p_searchKeys) {
                var _self = this;                                                             
                _self.showMid = false;
                document.getElementById("Home").className = "invalid-nav";
                _self.showLoad = true;
                _self.emptyResult = false;
                _self.videoList = [];
                _self.$http.post("/Home/GetVideoListsByFilter", { searchKeys: p_searchKeys }).then(function (res) {
                    for (var i = 0; i < res.body.length; i++) {
                        _self.emptyResult = false;
                        hasDataExist = true;
                        _self.videoList.push(res.body[i]);
                    }                   
                });

                _self.articList = [];
                _self.$http.post("/Home/GetArticleListsByFilter", { searchKeys: p_searchKeys }).then(function (res) {
                    for (var i = 0; i < res.body.length; i++) {
                        _self.emptyResult = false;
                        hasDataExist = true;
                        _self.articList.push(res.body[i]);
                    }                   
                });

                _self.guideList = [];
                _self.$http.post("/Home/GetGuideListsByFilter", { searchKeys: p_searchKeys }).then(function (res) {
                    for (var i = 0; i < res.body.length; i++) {
                        _self.emptyResult = false;
                        hasDataExist = true;
                        _self.guideList.push(res.body[i]);
                    }
                });

                _self.doctorList = [];
                _self.$http.post("/Home/GetDoctorInfoByFilter", { keyWord: p_searchKeys }).then(function (res) {
                    for (var i = 0; i < res.body.length; i++) {
                        _self.emptyResult = false;
                        if (res.status == 200) {
                            _self.doctorList.push(res.body[i]);
                        }
                    }
                    overflowEllipsis(_self);
                    if (_self.videoList.length === 0 && _self.articList.length === 0 && _self.guideList.length === 0 && _self.doctorList.length === 0) {
                        _self.emptyResult = true;
                    }

                    _self.showLoad = false;
                });
            },
            openBanner: function (event) {
                var index = 0;
                //IE doesn't pass in the event object
                event = event || window.event;
                //IE uses srcElement as the target
                var target = event.target || event.srcElement;
                index = event.target.parentNode.getAttribute("data-swiper-slide-index");
                //window.open('www.baidu.com');
                window.open(this.homeLogoImageList[index].Url);
            },
            toBanner: function () {
                //banner跳转链接
                var objArr = document.getElementsByClassName('swiper-wrapper');
                addHandler(objArr[0], 'click', this.openBanner);

                //objArr[0].addEventListener('click', function (event) {
                //    //IE doesn't pass in the event object
                //    event = event || window.event;
                //    //IE uses srcElement as the target
                //    var target = event.target || event.srcElement;
                //    index = event.target.parentNode.getAttribute("data-swiper-slide-index");
                //    //console.log(_self.homeLogoImageList[index].Url);
                //    window.open(_self.homeLogoImageList[index].Url);
                //});
            },
            getBanner: function () {
                //读取banner数据
                var _self = this;
                this.homeLogoImageList = [];
                this.$http.get("/Home/GetHomeLogoImages").then(function (res) {
                    for (var i = 0; i < res.body.length; i++) {
                        var homeLogoImage = {
                            ImgUrl: res.body[i].ImgUrl,
                            Url: res.body[i].Url,
                            TypeNumber: res.body[i].TypeNumber,
                            TitleName: res.body[i].TitleName
                        };
                        _self.homeLogoImageList.push(homeLogoImage);
                    }
                    //初始化banner图片幻影效果
                    _self.$nextTick(function () {
                        var swiper = new Swiper('.swiper-container', {
                            pagination: '.swiper-pagination',
                            observer: true,//修改swiper自己或子元素时，自动初始化swiper
                            observeParents: true,//修改swiper的父元素时，自动初始化swiper
                            paginationClickable: true,
                            autoplay: 5000,
                            loop: true,
                            autoplayDisableOnInteraction: false
                        });

                        _self.toBanner();
                    });
                });
            }
        },
        components: {
            'clinic-head': clinicHead,
            'clinic-right': clinicRight,
            'clinic-foot': clinicFoot,
            'clinic-process': clinicProcess
        },
    });
})(Vue)
